package com.phantasy.utils
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	/**
	 * FPS 监视类
	 * @author Liseen
	 * 
	 */	
	public class FPS extends Sprite
	{
		private static const tfDelayMax:int = 10;
		private static const maxMemory:uint = 41943000;
		private static const diagramHeight:uint = 40;
		private static const diagramWidth:uint = 62;
		
		
		private var _fpsTF:TextField;
		private var _memoryTF:TextField;
		private var _currentY:int;
		private var _diagram:BitmapData;
		private var _tfDelay:int=0;
		private var _tfTimer:int=0;
		private var _diagramTimer:int;
		private var _skins:int = -1;
		private var _skinsChanged:int = 0
		public function FPS()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);	
		}
		
		private function onAddToStageHandle(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);
			
			run();
		}
		
		private function run():void
		{
			_fpsTF = new TextField();
			_memoryTF = new TextField();
			this.mouseChildren = false;
			this.mouseEnabled = false;
			
			_fpsTF.defaultTextFormat = new TextFormat("Tahoma", 10, 0xFFFFFF);
			_fpsTF.autoSize = TextFieldAutoSize.LEFT;
			_fpsTF.text = ("FPS: " + Number(stage.frameRate).toFixed(2));
			_fpsTF.selectable = false;
			_fpsTF.x = 0;
			addChild(_fpsTF);
			_memoryTF.defaultTextFormat = new TextFormat("Tahoma", 10, 0xCCCC00);
			_memoryTF.autoSize = TextFieldAutoSize.LEFT;
			_memoryTF.text = ("MEM: " + bytesToString(System.totalMemory));
			_memoryTF.selectable = false;
			_memoryTF.x = 0;
			_memoryTF.y = 10;
			addChild(_memoryTF);
			
			var diaramBitMap:Bitmap;
			_currentY = 20;
			_diagram = new BitmapData(diagramWidth, diagramHeight, true, 553648127);
			diaramBitMap = new Bitmap(_diagram);
			diaramBitMap.y = _currentY+4;
			diaramBitMap.x = 0;
			addChildAt(diaramBitMap, 0);
			this.addEventListener(Event.ENTER_FRAME,onEnterFrameHandle);
			
		}
		
		private function onEnterFrameHandle(event:Event):void
		{
			_tfDelay++;
			if (_tfDelay >= tfDelayMax){
				_tfDelay = 0;
				_fpsTF.text = ("FPS: " + Number(((1000 * tfDelayMax) / (getTimer() - _tfTimer))).toFixed(2));
				_tfTimer = getTimer();
			}
			var timeLag:Number = (1000 / (getTimer() - _diagramTimer));
			var frameLag:Number = ((timeLag > stage.frameRate)) ? 1 : (timeLag / stage.frameRate);
			_diagramTimer = getTimer();
			_diagram.scroll(1, 0);
			_diagram.fillRect(new Rectangle(0, 0, 1, _diagram.height), 553648127);
			_diagram.setPixel32(0, (diagramHeight * (1 - frameLag)), 4291611852);
			_memoryTF.text = ("MEM: " + bytesToString(System.totalMemory));
			
			var skinLag:Number = ((_skins == 0)) ? 0 : (_skinsChanged / _skins);
			_diagram.setPixel32(0, (diagramHeight * (1 - skinLag)), 4294927872);
			var memLag:Number = (System.totalMemory / maxMemory);
			_diagram.setPixel32(0, (diagramHeight * (1 - memLag)), 4291611648);
		}
		
		private function bytesToString(mem:uint):String{
			var transformStr:String;
			if (mem < 0x0400){
				transformStr = (String(mem) + "b");
			} else {
				if (mem < 0x2800){
					transformStr = (Number((mem / 0x0400)).toFixed(2) + "kb");
				} else {
					if (mem < 102400){
						transformStr = (Number((mem / 0x0400)).toFixed(1) + "kb");
					} else {
						if (mem < 0x100000){
							transformStr = (Math.round((mem / 0x0400)) + "kb");
						} else {
							if (mem < 0xA00000){
								transformStr = (Number((mem / 0x100000)).toFixed(2) + "mb");
							} else {
								if (mem < 104857600){
									transformStr = (Number((mem / 0x100000)).toFixed(1) + "mb");
								} else {
									transformStr = (Math.round((mem / 0x100000)) + "mb");
								}
							}
						}
					}
				}
			}
			return transformStr;
		}
	}
}